package com.example.demo.action;

import com.example.demo.entity.Result;
import com.example.demo.entity.User;
import com.example.demo.entity.UserReq;
import com.example.demo.entity.UserResp;
import com.example.demo.service.IUserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by Hy on 2019/3/1.
 */
@RestController
@RequestMapping(value = "/api/user/",produces = {"application/json;charset=UTF-8"})
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private IUserService iUserService;

    @ApiOperation(value = "获取所有用户列表",notes = "获取用户列表")
    @RequestMapping(value = "findAll",method = RequestMethod.POST)
    public Result findAll(){
        List<User> list = iUserService.findList();
        UserResp users = new UserResp();
        users.setLists(list);
        return new Result.Builder<>(0).msg("成功").body(users).build();
    }

    @ApiOperation(value = "获取用户详细信息",notes = "根据url的id获取用户的详细信息")
    @ApiImplicitParam(name = "req",value="用户请求对象req",required = true,dataType = "UserReq")
    @RequestMapping(value = "findOne",method = RequestMethod.POST)
    public Result findOne(@RequestBody UserReq req){
        logger.info("传入的参数id:{}",req.getId());
        User user = iUserService.getUserById(req.getId());
        return new Result.Builder<>(0).msg("成功").body(user).build();
    }
}
